package cn.edu.scau.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.scau.Bean.ClassPage;
import cn.edu.scau.Bean.Classes;
import cn.edu.scau.Bean.Student;
import cn.edu.scau.Bean.StudentPage;
import cn.edu.scau.Jdbc.JdbcUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class StudentServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		// 转换页面
		String type = req.getParameter("type");
		if (type == null) {

			sendRed(req, resp);

		} else {

			printJason(req, resp, type);
		}
	}

	private void sendRed(HttpServletRequest req, HttpServletResponse resp) throws IOException {

		// 获得classid
		String classId = req.getParameter("classId");
		// 新建studentPage
		StudentPage studentPage = new StudentPage();
		// 转化为数字
		int id = Integer.parseInt(classId);
		// 获得这个班级所有的全部学生
		List<Student> students = JdbcUtils.getStudents(id);

		List<Student> showStudents = null;
		// 获得要显示的当前页的信息
		if (students.size() > 4) {
			showStudents = students.subList(0, 4);
		} else {
			showStudents = students.subList(0, students.size());
		}
		// 设置学生最大行数
		studentPage.setMaxRow(students.size());
		// 把学生当前页保存到session中
		req.getSession().setAttribute("studentPage", studentPage);
		// 把查询到的所有学生保存到session中
		req.getSession().setAttribute("students", students);
		// 把当前页要显示的信保存起来
		req.getSession().setAttribute("showStudents", showStudents);
		// 跳转到指定的页面
		resp.sendRedirect("/Tables/data/student.jsp?classId=" + classId);
	}

	/**
	 * 输出当前页面的学生信息
	 * 
	 * @param req
	 * @param resp
	 * @param type
	 * @throws IOException
	 */
	private void printJason(HttpServletRequest req, HttpServletResponse resp, String type) throws IOException {

		// 获得学生当前的页面
		StudentPage studentPage = (StudentPage) req.getSession().getAttribute("studentPage");

		List<Student> students = (List<Student>) req.getSession().getAttribute("students");
		switch (type) {

		case "first":
			studentPage.setCurrentPage(1);
			break;
		case "pre":
			if (studentPage.getCurrentPage() != 1) {
				studentPage.setCurrentPage(studentPage.getCurrentPage() - 1);
			}
			break;
		case "next":
			if (studentPage.getCurrentPage() != studentPage.getMaxPage()) {
				studentPage.setCurrentPage(studentPage.getCurrentPage() + 1);
			}
			break;
		case "last":
			studentPage.setCurrentPage(studentPage.getMaxPage());
			break;

		}

		int start = (studentPage.getCurrentPage() - 1) * 4;

		int end = studentPage.getCurrentPage() * 4;

		if (end > studentPage.getMaxRow()) {
			end = studentPage.getMaxRow();
		}

		List<Student> showStudents = students.subList(start, end);

		// 把学生当前页保存到session中
		req.getSession().setAttribute("studentPage", studentPage);
		// 把当前页要显示的信保存起来
		req.getSession().setAttribute("showStudents", showStudents);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("students", showStudents);
		JSONArray jsonArray = new JSONArray();
		jsonArray.add(jsonObject);

		req.getSession().setAttribute("classes", showStudents);

		PrintWriter pw = resp.getWriter();
		System.out.println(jsonArray.toString());
		pw.write(jsonArray.toString());
	}
}
